//CESEconomics
log using CESEconomics.log, replace

use "C:\Users\sbstjp\OneDrive - Cardiff University\CES24_Common.dta" // Schaffner, Brian; Shih, Marissa; Ansolabehere, Stephen; Pope, Jeremy, 2025, "Cooperative Election Study Common Content, 2024", https://doi.org/10.7910/DVN/X11EP6, Harvard Dataverse, V3. Accessed on 12 April 2025.

// Social Justice scale
rename CC24_321e defund
rename CC24_444a gendertransition
rename CC24_444b parentalconsentpronouns 
rename CC24_445a raceuniadmissions

*Reverse coding so social justice values are high
foreach var in defund {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in rdefund parentalconsentpronouns gendertransition raceuniadmissions {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in rdefund parentalconsentpronouns gendertransition raceuniadmissions  {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Economic variables
*rename
rename CC24_301 Nationaleconomy
rename CC24_302 HouseholdIncomeDecrease 
rename CC24_303 pricechange 
rename CC24_305_2 lostajob 
rename CC24_305_12 raiseatwork 
rename CC24_305_13 paycutatwork 
rename CC24_309d_8 payemergency 
rename healthins_6 nohealthins 

*Delete missing values
replace Nationaleconomy=. if Nationaleconomy==6 
replace faminc_new=. if faminc_new==97 

*Generate dummies
gen unemployed=.
replace unemployed=1 if inlist(employ, 3, 4)
replace unemployed=0 if inlist(employ, 1, 2)

gen nothomeowner=.
replace nothomeowner=0 if ownhome==1
replace nothomeowner=1 if inlist(ownhome, 2, 3)

*Reverse coding so economic precarity is coded low
foreach var in unemployed nothomeowner Nationaleconomy HouseholdIncomeDecrease raiseatwork {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

// Correlations
pwcorr SocJusValues runemployed rnothomeowner rNationaleconomy rHouseholdIncomeDecrease rraiseatwork pricechange lostajob paycutatwork payemergency nohealthins faminc_new [aweight=commonweight], sig

log close

